import java.util.*;
import java.util.stream.Collectors;

/**
 * 多级分组
 * @author xiexiaojin
 * @date 2024-11-08 16:49
 */
public class DuoJiFenZu {
    public static void main(String[] args) {
        // 模拟数据库查询结果
        List<Map<String, String>> dataList = Arrays.asList(
                createData("2023-05-26", "123456", "111111", "粤菜", "222222", "毛血旺"),
                createData("2023-05-26", "123456", "111111", "粤菜", "333333", "红烧肉"),
                createData("2023-05-26", "123456", "222222", "川菜", "444444", "麻辣烫"),
                createData("2023-05-27", "123456", "111111", "粤菜", "555555", "叉烧")
        );

        // 按 date 分组
        Map<String, Map<String, Map<String, List<Map<String, String>>>>> groupedData = dataList.stream()
                .collect(Collectors.groupingBy(
                        data -> (String) data.get("date"),
                        Collectors.groupingBy(
                                data -> (String) data.get("mealTypeId"),
                                Collectors.groupingBy(
                                        data -> (String) data.get("dishesTypeId")
                                )
                        )
                ));

        // 将分组数据转换为所需的 JSON 结构
        List<Map<String, String>> result = new ArrayList<>();
        groupedData.forEach((date, mealTypeMap) -> {
            Map<String, String> dateMap = new HashMap<>();
            dateMap.put("date", date);

            List<Map<String, String>> mealList = new ArrayList<>();
            mealTypeMap.forEach((mealTypeId, dishesTypeMap) -> {
                Map<String, String> mealMap = new HashMap<>();
                mealMap.put("mealTypeId", mealTypeId);

                List<Map<String, String>> dishesList = new ArrayList<>();
                dishesTypeMap.forEach((dishesTypeId, dishList) -> {
                    Map<String, String> dishesMap = new HashMap<>();
                    dishesMap.put("dishesTypeId", dishesTypeId);
                    dishesMap.put("dishesTypeName", dishList.get(0).get("dishesTypeName"));

                    List<Map<String, String>> releaseDishesList = dishList.stream()
                            .map(dish -> {
                                Map<String, String> dishMap = new HashMap<>();
                                dishMap.put("dishid", dish.get("dishid"));
                                dishMap.put("dishesName", dish.get("dishesName"));
                                return dishMap;
                            })
                            .collect(Collectors.toList());

                    dishesMap.put("releaseDishesList", releaseDishesList.toString());
                    dishesList.add(dishesMap);
                });

                mealMap.put("dishesList", dishesList.toString());
                mealList.add(mealMap);
            });

            dateMap.put("mealList", mealList.toString());
            result.add(dateMap);
        });

        // 使用 Gson 库将分组数据转换为 JSON
//        Gson gson = new GsonBuilder().setPrettyPrinting().create();
//        String jsonResult = gson.toJson(result);
        System.out.println(result.toString());
    }

    // 模拟数据库返回数据
    private static Map<String, String> createData(String date, String mealTypeId, String dishesTypeId, String dishesTypeName, String dishid, String dishesName) {
        Map<String, String> data = new HashMap<>();
        data.put("date", date);
        data.put("mealTypeId", mealTypeId);
        data.put("dishesTypeId", dishesTypeId);
        data.put("dishesTypeName", dishesTypeName);
        data.put("dishid", dishid);
        data.put("dishesName", dishesName);
        return data;
    }
}

